# coding:utf-8

import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
from tornado.options import options, define
from tornado.web import RequestHandler, MissingArgumentError
#
# define("port", default=8000, type=int, help="run server on the given port.")
#
# class IndexHandler(RequestHandler):
#     def post(self):
#         # query_arg = self.get_query_argument("a")
#         # #返回的是一个列表，参数a的列表
#         # query_args = self.get_query_arguments("a")
#         # body_arg = self.get_body_argument("a")
#         # #strip表示去掉两边空格！
#         # #获取请求提参数！不能处理json数据
#         # body_args = self.get_body_arguments("a", strip=False)
#
#         #前两种的整合使用方式！
#         arg = self.get_argument("a")
#         args = self.get_arguments("a")
#
#         default_arg = self.get_argument("b", "hello")
#         default_args = self.get_arguments("b")
#
#         try:
#             missing_arg = self.get_argument("c")
#         except MissingArgumentError as e:
#             missing_arg = "We catched the MissingArgumentError!"
#             print(e)
#         missing_args = self.get_arguments("c")
#
#         # rep = "query_arg:%s<br/>" % query_arg
#         # rep += "query_args:%s<br/>" % query_args
#         # rep += "body_arg:%s<br/>"  % body_arg
#         # rep += "body_args:%s<br/>" % body_args
#         rep = "arg:%s<br/>"  % arg
#         rep = "args:%s<br/>" % args
#         rep += "default_arg:%s<br/>" % default_arg
#         rep += "default_args:%s<br/>" % default_args
#         rep += "missing_arg:%s<br/>" % missing_arg
#         rep += "missing_args:%s<br/>" % missing_args
#
#         self.write(rep)
#
# if __name__ == "__main__":
#     tornado.options.parse_command_line()
#     app = tornado.web.Application([
#         (r"/", IndexHandler),
#     ])
#     http_server = tornado.httpserver.HTTPServer(app)
#     http_server.listen(options.port)
#     tornado.ioloop.IOLoop.current().start()




'''
RequestHandler.request 对象存储了关于请求的相关信息，具体属性有：
    method HTTP的请求方式，如GET或POST;
    host 被请求的主机名；
    uri 请求的完整资源标示，包括路径和查询字符串；
    path 请求的路径部分；
    query 请求的查询字符串部分；
    version 使用的HTTP版本；
    headers 请求的协议头，是类字典型的对象，支持关键字索引的方式获取特定协议头信息，例如：request.headers["Content-Type"]
    body 请求体数据；
    remote_ip 客户端的IP地址；
    files 用户上传的文件，为字典类型，型如：
        {
            "form_filename1":[<tornado.httputil.HTTPFile>, <tornado.httputil.HTTPFile>],
            "form_filename2":[<tornado.httputil.HTTPFile>,],
            ... 
        }
'''
# coding:utf-8

import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
from tornado.options import options, define
from tornado.web import RequestHandler

define("port", default=8000, type=int, help="run server on the given port.")

class IndexHandler(RequestHandler):
    def get(self):
        self.write("hello itcast.")

class UploadHandler(RequestHandler): 
    def post(self):
        files = self.request.files
        img_files = files.get('img')
        if img_files:
            img_file = img_files[0]["body"]
            file = open("./itcast", 'w+')
            file.write(img_file)
            file.close()
        self.write("OK")

if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application([
        (r"/", IndexHandler),
        (r"/upload", UploadHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()
